<?php
namespace cubox\provider;

class Response{

    public static function write($data){
        if (is_array($data)){
            var_dump($data);
        }else{
            echo $data;
        }
    }

    public static function error($msg,$content = null){
        echo '<h1>'.$msg.'</h1>';
        if (gettype($content)=='object'){
                
            if (in_array("Exception",class_parents($content))| get_class($content) == "Exception"){
                
                echo '<div>'.$content->getCode().'</div>';
                echo '<div>'.$content->getMessage().'</div>';
                echo '<div>'.$content->getFile().' Line :'.$content->getLine().'</div>';
                echo '</br>';
                foreach ($content->getTrace() as $key => $value) {
                    echo  '<div>'.self::getTraceStr($value).'</div>';
                }
            }else{
                $content = print_r($content,true);
            }
        }elseif (is_array($content)){
            echo print_r($content,true);
        }else{
            echo '<div>'.$content.'</div>';
        }
    }

    private static function getTraceStr($value){
        $str = '';
        if (isset($value['file'])){
            $str .= $value['file'].' Line:'.$value['line'].' ';
        }

        if (isset($value['function'])){
            if (isset($value['class'])){
                $str .= $value['class'].$value['type'].$value['function'];
            }else{
                $str .= $value['function'];
            }
        }
        return $str;
    }
}